﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace dianyingyuan
{
    public partial class MianForm : Form
    {
        Movie movie = null;
        /// <summary>
        /// 所有放映计划的集合
        /// </summary>
        Dictionary<string, Plan> myPlan = null;

        /// <summary>
        /// 所有电影的集合
        /// </summary>
        Dictionary<string, Movie> myMovies = new Dictionary<string, Movie>();
        public MianForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            //this.tsmiSiero.Enabled = false;

            //InitMovies();
            //CreateMovieTree();
            //this.tsmiShuaxin.Enabled = false;

            //BinaryFormatter bf = new BinaryFormatter();
            //FileStream fs = null;
            //try
            //{
            //    fs = new FileStream("Sava.txt", FileMode.Open);
            //    this.myPlan = bf.Deserialize(fs) as Dictionary<string, Plan>;
            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show(ex.Message);
            //}
            //finally
            //{
            //    fs.Close();
            //}


        }
        /// <summary>
        /// 读取XML 做电影对象 传到 电影集合
        /// </summary>
        public void InitMovies()
        {
            //读XML文件
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("ShowList.xml");
            XmlNode showListNode = xmlDocument.DocumentElement;

            foreach (XmlNode movieNode in showListNode.ChildNodes)
            {
                //每部电影的信息
                string movieName = movieNode.ChildNodes[0].InnerText;
                string picture = movieNode.ChildNodes[1].InnerText;
                string director = movieNode.ChildNodes[2].InnerText;
                string actor = movieNode.ChildNodes[3].InnerText;

                MovieType type
                     = (MovieType)(Enum.Parse(typeof(MovieType),
                     movieNode.ChildNodes[4].InnerText));
                int price = int.Parse(movieNode.ChildNodes[5].InnerText);

                List<string> showTimeList = new List<string>();
                foreach (XmlNode time in movieNode.ChildNodes[6].ChildNodes)
                {
                    showTimeList.Add(time.InnerText);
                }

                Movie movie = new Movie(movieName, picture, director, actor, type,
                     price, showTimeList);

                this.myMovies.Add(movie.MovieName, movie);


            }
        }
        /// <summary>
        /// 循环集合 挂到 树上
        /// </summary>
        private void CreateMovieTree()
        {
            TreeNode treeRoot = new TreeNode("放映计划");
            treeRoot.Tag = "放映计划";

            foreach (string key in this.myMovies.Keys)
            {
                TreeNode movieNameNode = new TreeNode(this.myMovies[key].MovieName);
                movieNameNode.Tag = "影片";

                foreach (string showTime in this.myMovies[key].ShowTimeList)
                {
                    TreeNode showTimeNode = new TreeNode(showTime);
                    showTimeNode.Tag = "放映时间";

                    movieNameNode.Nodes.Add(showTimeNode);
                }

                treeRoot.Nodes.Add(movieNameNode);
            }

            this.twPlan.Nodes.Add(treeRoot);

            this.twPlan.ExpandAll();
        }
        /// <summary>
        /// 点击树上节点 在详情那里 显示 相应电影的 详细信息
        /// </summary>
        /// <param name="mo"></param>
        private void ShowMovieInfo(Movie mo)
        {
            lbldaoyan.Text = mo.Director;
            lbllei.Text = mo.MovieType.ToString();
            lblpian.Text = mo.MovieName;
            lblzhuyan.Text = mo.Actor;
            lbljiage.Text = mo.Price.ToString();
            pictureBox1.Image = Image.FromFile(mo.Picture);
            string time = "";
            foreach (string ti in mo.ShowTimeList)
            {
                time += " " + ti;
            }
            lbltime.Text = time;
        }

         private void twPlan_AfterSelect(object sender, TreeViewEventArgs e)
         {

             int num = 0;
             int all = 0;

             //选中电影名节点
             if (this.twPlan.SelectedNode.Tag.ToString() == "影片")
             {
                 label4.Text = twPlan.SelectedNode.Text;
                 label6.Text = "";
                 label7.Text = "";
                 movie = this.myMovies[this.twPlan.SelectedNode.Text];
                 this.tpShow.Controls.Clear();


                 foreach (string time in myMovies[label4.Text].ShowTimeList)
                 {
                     for (int i = 1; i <= 4; i++)
                     {
                         for (int j = 1; j <= 8; j++)
                         {
                             Color h = myPlan[label4.Text + "-" + time].Seats[i + "-" + j].Color;
                             if (h == Color.Red)
                             {
                                 num++;
                                 
                             }
                             all = twPlan.SelectedNode.GetNodeCount(true) * 32;



                         }
                     }
                 }
                 //显示电影
                 this.ShowMovieInfo(movie);
             }
             //选中时间节点
             else if (this.twPlan.SelectedNode.Tag.ToString() == "放映时间")
             {

                 movie = this.myMovies[this.twPlan.SelectedNode.Parent.Text];
                 label6.Text = "";
                 label7.Text = "";


                 string showTime = this.twPlan.SelectedNode.Text;
                 //显示座位
                 foreach (Plan plan in this.myPlan.Values)
                 {
                     string[] msg = plan.PlanName.Split('-');

                     if (msg[1] == showTime)
                     {
                         this.DrawSeats(plan);

                         break;
                     }
                 }


                 for (int i = 1; i <= 4; i++)
                 {
                     for (int j = 1; j <= 8; j++)
                     {
                         Color h = myPlan[label4.Text].Seats[i + "-" + j].Color;
                         if (h == Color.Red)
                         {
                             num++;
                             
                         }
                         all = 32;



                     }
                 }


                 //显示电影
                 this.ShowMovieInfo(movie);



             }
             else
             {
                 label4.Text = "";
                 label6.Text = "";
                 label7.Text = "";

                 foreach (string key in myMovies.Keys)
                 {
                     foreach (string time in myMovies[key].ShowTimeList)
                     {
                         for (int i = 1; i <= 4; i++)
                         {
                             for (int j = 1; j <= 8; j++)
                             {
                                 Color h = myPlan[key + "-" + time].Seats[i + "-" + j].Color;
                                 if (h == Color.Red)
                                 {
                                     num++;
                                     
                                 }
                                 all = myPlan.Count * 32;



                             }
                         }
                     }


                 }



             }
            
             int yu = all - num;
             label6.Text = num.ToString() + "张";
             label6.Tag = num;
             label7.Text = yu.ToString() + "张";
             label7.Tag = yu;

            

         }

        /// <summary>
        /// 初始化放映计划集合
        /// </summary>
        private void InitCinema()
        {
            //循环每一部电影
            foreach (Movie movie in this.myMovies.Values)
            {
                string movieName = movie.MovieName;

                //循环每部电影的放映时间
                foreach (string showTime in movie.ShowTimeList)
                {
                    string planName = movieName + "-" + showTime;

                    //产生座位集合
                    Dictionary<string, Seat> tempSeats =
                        new Dictionary<string, Seat>();

                    for (int i = 1; i <= 5; i++)
                    {
                        for (int j = 1; j <= 8; j++)
                        {
                            Seat seat = new Seat(i + "-" + j, Color.Yellow);

                            tempSeats.Add(seat.SeatNum, seat);
                        }
                    }



                    //创建放映计划对象
                    Plan plan = new Plan(planName, tempSeats);

                    //加入放映计划对象
                    this.myPlan.Add(plan.PlanName, plan);
                }
            }




        }

        /// <summary>
        /// 描绘每一场次的座位标签
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="page"></param>
        private void DrawSeats(Plan plan)
        {

            this.tpShow.Controls.Clear();

            int row = 0;
            int col = 0;
            label4.Text = plan.PlanName;
            
            //循环座位号
           foreach (Seat seat in this.myPlan[plan.PlanName].Seats.Values)
            {

          
                Label label = new Label();
                label.BackColor = seat.Color;
                label.AutoSize = false;
                label.Font = new Font("Arial", 16, FontStyle.Regular);
                label.Size = new Size(60, 30);
                label.Name = seat.SeatNum;
                label.Text = seat.SeatNum;
                label.TextAlign = ContentAlignment.MiddleCenter;
                label.Location = new Point(30 + (col * 90), 40 + (row * 60));

                label.Tag = plan.PlanName;

                //点击事件由买票方法处理
                label.Click += new EventHandler(this.BuyTicket);

                this.tpShow.Controls.Add(label);

                col++;

                if (col > 7)
                {
                    row++;
                    col = 0;
                }

            }

           

        }
        public void BuyTicket(object sender, EventArgs e) 
        {
            
            Label label = sender as Label;
            
            string num = label.Text;
            string[] movie = label.Tag.ToString().Split('-');
            Movie name = this.myMovies[movie[0]];
            string time = movie[1];
            //比较当前时间和 放映时间
            DateTime date1 = Convert.ToDateTime(time);
            DateTime date2 = Convert.ToDateTime(label5.Text);

            if(DateTime.Compare(date2,date1)>0)
            {
                MessageBox.Show("电影以开场，不能购票");
                return;
            }

            string zuo = label.Text;
            Ticket t = null;
            if (label.BackColor == Color.Red)
            {
                DialogResult haha = MessageBox.Show("您确定取消该座位票么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if (haha == DialogResult.OK)
                {
                    label.BackColor = Color.Yellow;
                    myPlan[label.Tag.ToString()].Seats[label.Name.ToString()].Color = Color.Yellow;
                    label6.Text = Convert.ToInt32(label6.Tag) - 1 + "张";
                    label6.Tag = Convert.ToInt32(label6.Tag) - 1;

                    label7.Text = Convert.ToInt32(label7.Tag) + 1 + "张";
                    label7.Tag = Convert.ToInt32(label7.Tag) + 1;
                    return;
                }
                else 
                {
                    return;
                }
                
            }
            if(rdoputong.Checked)
            {
               
                DialogResult haha = MessageBox.Show("您确定购买 "+zuo+" 座位票么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                if (haha == DialogResult.OK)
                {
                    t = fictor.haha("pu", num, name, 80, time, "", 0);
                    try
                    {
                        myPlan[label.Tag.ToString()].Seats[label.Name.ToString()].Color = Color.Red;
                        label.BackColor = Color.Red;
                        t.printsa();
                        MessageBox.Show("票以打印");
                        label6.Text = Convert.ToInt32(label6.Tag) + 1 + "张";
                        label6.Tag = Convert.ToInt32(label6.Tag) + 1;

                        label7.Text = Convert.ToInt32(label7.Tag) - 1 + "张";
                        label7.Tag = Convert.ToInt32(label7.Tag) - 1;
                        return;
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
                else 
                {
                    return;
                }
                
            }
            else if (rdozeng.Checked)
            {
                if (txtEng.Text == "")
                {
                    MessageBox.Show("请填写赠送者");
                }
                else 
                {
                    myPlan[label.Tag.ToString()].Seats[label.Name.ToString()].Color = Color.Red;
                    label.BackColor = Color.Red;
                    t =fictor.haha("zeng",num, name, 90, time, txtEng.Text,0);

                    ZengTicket ze = t as ZengTicket;
                    ze.printsa();
                    MessageBox.Show("票以打印");
                    label6.Text = Convert.ToInt32(label6.Tag) + 1 + "张";
                    label6.Tag = Convert.ToInt32(label6.Tag) + 1;

                    label7.Text = Convert.ToInt32(label7.Tag) - 1 + "张";
                    label7.Tag = Convert.ToInt32(label7.Tag) - 1;
                    return;
                }
                


            }
            else if (rdostu.Checked)
            {
                if (cboze.Text == "")
                {
                    MessageBox.Show("请选择折扣");
                }
                else 
                {
                    myPlan[label.Tag.ToString()].Seats[label.Name.ToString()].Color = Color.Red;
                    label.BackColor = Color.Red;
                    string[] jia = cboze.SelectedItem.ToString().Split(' ');
                    int jiage = int.Parse(jia[0]);
                    t =fictor.haha("stu",num, name, 80, time, "",jiage);
                    StudentTicket stutic = t as StudentTicket;
                    stutic.CalPrice();
                    stutic.printsa();
                    MessageBox.Show("票以打印");
                    label6.Text = Convert.ToInt32(label6.Tag) + 1 + "张";
                    label6.Tag = Convert.ToInt32(label6.Tag) + 1;

                    label7.Text = Convert.ToInt32(label7.Tag) - 1 + "张";
                    label7.Tag = Convert.ToInt32(label7.Tag) - 1;
                    return;
                }
               
            }

            else 
            {
                MessageBox.Show("请选择票类型");
                return;
                
            }
            
            
        }

        private void rdoputong_CheckedChanged(object sender, EventArgs e)
        {
           RadioButton btn= sender as RadioButton;
           if (btn.Checked)
           {
               cboze.Enabled = false;
               txtEng.Enabled = false;
           }
           else 
           {
               cboze.Enabled = true;
               txtEng.Enabled = true;
           }
            
        }

        private void rdozeng_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btn2 = sender as RadioButton;
            if (btn2.Checked)
            {
                cboze.Enabled = false;
                
            }
            else
            {
                cboze.Enabled = true;
               
            }
        }

        private void rdostu_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btn3 = sender as RadioButton;
            if (btn3.Checked)
            {
               
                txtEng.Enabled = false;
            }
            else
            {
                
                txtEng.Enabled = true;
            }
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
            //运用序列化保存放映计划集合
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("Sava.txt", FileMode.Create);

            try
            {
                bf.Serialize(fs, this.myPlan);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

            finally 
            {
                if (fs != null) 
                {
                    fs.Close();
                    
                }
            }

            Application.Exit();
        }

       

        private void tsmiSiero_Click(object sender, EventArgs e)
        {
            this.tsmiShuaxin.Enabled = false;
            this.myPlan = new Dictionary<string, Plan>();
            InitMovies();
            CreateMovieTree();
            InitCinema();
            this.tsmiSiero.Enabled = false;
        }

        private void tsmiShuaxin_Click(object sender, EventArgs e)
        {
            this.tsmiSiero.Enabled = false;
            
            InitMovies();
            CreateMovieTree();
            this.tsmiShuaxin.Enabled = false;

            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = null;
            try
            {
                fs = new FileStream("Sava.txt", FileMode.Open);
                this.myPlan = bf.Deserialize(fs) as Dictionary<string, Plan>;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                fs.Close();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           // label5.Text = System.DateTime.Now.ToString();
        
            label5.Text = System.DateTime.Now.ToString("HH:mm:ss");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num = 0;
            //通过电影名 找时间表


            //选中电影名节点
            if (this.twPlan.SelectedNode.Tag.ToString() == "影片")
            {



                foreach (string time in myMovies[label4.Text].ShowTimeList)
                {
                    for (int i = 1; i <= 4; i++)
                    {
                        for (int j = 1; j <= 8; j++)
                        {
                            Color h = myPlan[label4.Text + "-" + time].Seats[i + "-" + j].Color;
                            if (h == Color.Red)
                            {
                                num++;
                            }



                        }
                    }
                }
            }
            //选中时间节点
            else if (this.twPlan.SelectedNode.Tag.ToString() == "放映时间")
            {
                for (int i = 1; i <= 4; i++)
                {
                    for (int j = 1; j <= 8; j++)
                    {
                        Color h = myPlan[label4.Text].Seats[i + "-" + j].Color;
                        if (h == Color.Red)
                        {
                            num++;
                        }



                    }
                }
            }
            else
            {
                foreach (string key in myMovies.Keys)
                {
                    foreach (string time in myMovies[key].ShowTimeList)
                    {
                        for (int i = 1; i <= 4; i++)
                        {
                            for (int j = 1; j <= 8; j++)
                            {
                                Color h = myPlan[key + "-" + time].Seats[i + "-" + j].Color;
                                if (h == Color.Red)
                                {
                                    num++;
                                }



                            }
                        }
                    }


                }
            }

            int all = 32;
            int yu = all - num;
            label6.Text = num.ToString() + "张";
            label7.Text = yu.ToString() + "张";








        }

       
    }
}